<script lang="ts" setup>
import { computed, h, reactive, toRefs } from 'vue'
import { DownloadOutlined } from '@ant-design/icons-vue'
import Api from '@/api/'
import to from '@/utils/awaitTo'
import { download } from '@/utils/utils'
import { message } from 'ant-design-vue'


// 引用声明
const props = defineProps({
  row: {
    type: Object,
    default: () => {
    }
  }
})

// 引用事件
const emits = defineEmits(['callback'])

// 响应式数据
const state = reactive({
  loading: false
})

// 响应式数据解析
const { loading } = toRefs(state)

// 下载视频
const handleDownload = async () => {
  const params = {
    videoIds: [],
    voiceOrVideoOrProductionRequirement: 2
  }
  params.videoIds.push(props.row.videoId)

  const [error, data] = await to(Api.video.videoDownLoad(params))

  if (!error) {
    const [urlList] = data

    console.log(urlList)

    if (urlList) {
      const {
        url: { originalFilmUrl }
      } = urlList
      const dothis = []
      dothis.push(props.row.videoId)

      console.log(dothis)

      loading.value = true
      const [error, data] = await to(Api.video.videoDownLoadStatus(dothis))
      loading.value = false
      if (!error) {
        download(originalFilmUrl, props.row.topicTitle)
        console.log(data)
        message.success('状态已更新')
        emits('callback')
      }
    } else {
      download(props.row.originalFilmUrl, props.row.topicTitle)
    }
  }
}

// 是否有数智人
const isUrl = computed(() => {
  return props.row.originalFilmUrl
})
</script>

<template>
  <div v-if="isUrl">
    <a-button v-if="Number(row.downloadStatus) === 3001" :icon="h(DownloadOutlined)" :loading="loading" type="primary"
              @click="handleDownload">重新下载
    </a-button>
    <a-button v-else :icon="h(DownloadOutlined)" :loading="loading" type="primary" @click="handleDownload">下载视频
    </a-button>
  </div>
</template>

<style lang="less" scoped>

</style>
